<?php
namespace Api\Controller;
use Think\Controller;


/** 
 * 用户栏目的接口定义
 * @author Legend. <xcx_legender@qq.com>
 * @param 
 * @return 
 */

class TopicController extends PublicController 
{

	/** 
	 * [设置用户的栏目信息]
	 * @author Legend. <xcx_legender@qq.com>
	 * @param 
	 * @return 
	 */
	public function set($ids = '', $appToken = '')
	{
		$uid = $this->getUid($appToken);
		if (!$uid)
		{
			return $this->login_error();
			#todo test
			// $uid = 2;
		}
		if (!$ids) $this->param_error();
		$ids = explode(',', $ids);
		event('Topic')->setUserTopics($uid, $ids);
		$this->success();
	}

	//获取用户的栏目
	public function mine($appToken = '')
	{
		$uid = $this->getUid($appToken);
		if (!$uid)
		{
			return $this->login_error();
			#todo test
			// $uid = 2;
		}
		$topics = event('Topic')->getUserTopics($uid);
		if (count($topics) == 0)
		{
			$topics = event('Topic')->getHotTopics();
		}
		$this->list_success(array_values($topics));
	}

	/** 
	 * [获取所有的栏目信息]
	 * @author Legend. <xcx_legender@qq.com>
	 * @param 
	 * @return 
	 */
	public function all()
	{
		$list = A('Api/Topic', 'Event')->lists();
		$list = field_filter($list, ['id', 'title'], true);
		$list = array_column($list, null, 'id');

		$hot = event('Topic')->getHotTopics();
		foreach ($hot as $key => $value) {
			unset($list[$key]);
		}
		$list = array_merge($hot, $list);
		$this->list_success($list);
	}

	// 获取栏目下的视频
	public function videos($id = 0, $games = '')
	{
		$games = $games ? explode(',', $games) : array();
		if (!$id)
		{
			$this->success(array());
		}
		$vids = A('Api/Video', 'Event')->getByTopicAndGame($id, $games, I('timestamp', 0) , I('limit' , ''));
		$videos = $this->lists_format($vids);
		$data = $this->formatMVideos($videos);
		$this->list_success($data);

	}

	/**
	 * APP推荐数据
	 * 编辑选推前十
	 * @author liubo <liubo@ilongyuan.com.cn>
	 */
	public function recommend($games = '') {
		$topicId = 3;
		$games = $games ? explode(',', $games) : array();

		$vids = A('Api/Video', 'Event')->getByTopicAndGame($topicId, $games, 0, 10);
		$videos = $this->lists_format($vids);
		$data = $this->formatMVideos($videos);
		$this->list_success($data);
	}

	/**
	 * 热门视频接口
	 * @author liubo <liubo@ilongyuan.com.cn>
	 */
	public function hot($games = '') {
		$games = $games ? explode(',', $games) : [];
		$vids = event('VideoHot')->getData($games, I('p', 0, 'intval'), I('pager', 10, 'intval'));

		$videos = $this->lists_format($vids);
		$data = $this->formatMVideos($videos);
		$this->list_success($data);
	}


	/**
	 * APP栏目视频 新接口 2016-07-04
	 * @author liubo <liubo@ilongyuan.com.cn>
	 */
	public function videolist() {
		$topic_id = I('id', 0, 'intval');
		if (empty($topic_id)) {
			$this->success(array());
		}
		$appToken = I('appToken', '', 'trim');
		$appToken && $uid = $this->getUid($appToken);

		if (!empty($uid)) {
			$games = event('GameScribe')->mine($uid);
		} else {
			$games = I('games', '', 'trim');
			$games = $games ? explode(',', $games) : [];
			$games = array_map('intval', $games);
		}

		$p = I('p', 1, 'intval');
		$timestamp = I('timestamp', 0, 'intval');
		$limit = I('limit', 10, 'intval');

		// $limit最大值20
		$limit > 20 && $limit = 20;

		$vEvent = event('Video');

		switch ($topic_id) {
			case 4:
				// 逛一逛
				isset($games) && $map['game_id'] = ['in', $games];
				$vids = $vEvent->randomLists($map, $limit);
				break;
			case 5:
				// 我的关注
				$vids = $this->_follow($uid, $games, $timestamp, $limit);
				break;
			case 6:
				// 热门视频
				$vids = event('VideoHot')->getData($games, $p, $limit);
				break;
			case 7:
				// 手机专区
                $map = ['game_id' => 9];
                $vids = event('Video')->getVideos($map, $p, $limit);
                break;
			default:
				$vids = $vEvent->getByTopicAndGame($topic_id, $games, $timestamp, $limit);
				break;
		}

		$videos = $this->lists_format($vids);
		$data = $this->formatMVideos($videos);
		$this->list_success($data);
	}

	/**
	 * 我的关注数据
	 * @author liubo <liubo@ilongyuan.com.cn>
	 */
	private function _follow($uid = 0, $games = [], $timestamp = 0, $limit = 10) {
		$vService = service('Video');

		if ($uid) {
			$fEvent = event('Follow');
			$follow_uids = $fEvent->mine($uid);
		}
		$query[] = array(
			'recommend' => $vService::RECOMMEND_YES,
		);
		!empty($follow_uids) && $query[]['uid'] = array('$in' => $follow_uids);
		!empty($games) && $query[]['game_id'] = array('$in' => $games);
		$map['$and'][]['$or'] = $query;
		$map['$and'][]['create_time'] = $timestamp ? array('$lt' => $timestamp) : array('$gt' => 0);
		$map['$and'][]['status'] = $vService::STATUS_PASS;
		$map['$and'][]['race_id'] = array('$eq' => null); ////非比赛视频
		$where['_array'] = $map;
		$res = $vService->where($where)->order('create_time desc')->limit($limit)->field('id')->select();
		$vids = [];
		foreach ($res as $value) {
			$vids[] = $value['id'];
		}
		return $vids;
	}
}